package com.gjy.concurrency.clazz;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.*;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-18 14:19:27
 */
public class CountDownLatchTest {

    @Test
    public void test1() throws InterruptedException, ExecutionException {
        ExecutorService pool = Executors.newFixedThreadPool(1);

        CountDownLatch cdl = new CountDownLatch(5);

        for (int i = 0; i < 5; i++) {
            Future<String> future = pool.submit(new C1(cdl));
            System.out.println("future.get() = " + future.get());
        }

        cdl.await();
    }

    private static final class C1 implements Callable<String> {

        private final CountDownLatch cdl;

        public C1(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public String call() throws Exception {
            try {
                cdl.countDown();
                return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
